﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class WakeUpConfigIdiot : WakeUpConfigStartPos{

    public float Health = 500;
    public WakeUpConfigIdiot(Vector2 pos,float health) :base(pos){
        this.Health = health;
    }
}



public class IDiot : PrefabCommonBase
{

    // ----- 对象引用 -----
    public MotionStateMgr MotionState_ = null;

    public Rigidbody2D Rigid2d_ = null;

    // ----- 数据属性 -----
    private float health_ = 500;
    private int moveOriState_ = CmdMove.ORI_LEFT;
    private void Update()
    {
        if (this.State == EnumItemState.E_ACTIVE)
        {
            this.GetComponent<ActorHuman>().SendCmd(new CmdMove(moveOriState_));
        }
    }



    private void switchMoveOri()
    {
        moveOriState_ = (moveOriState_ == CmdMove.ORI_LEFT) ? CmdMove.ORI_RIGHT : CmdMove.ORI_LEFT;
    }

    private bool changeHealth(float changeValue) {
        this.health_ = Mathf.Max(0,this.health_ - changeValue);
        return this.health_ > 0;
    }


    // ----- 处理一系列的事件 -----

    public override void SetStatusByConfig(WakeUpConfigBase config)
    {
        WakeUpConfigIdiot wc = config as WakeUpConfigIdiot;
         if (wc != null) {
            this.transform.localPosition = new Vector3(wc.StartPos.x,wc.StartPos.y, this.transform.localPosition.z);
            this.health_ = wc.Health;
         }
    }

    protected override void onActive()
    {
        this.gameObject.layer = LayerMgrs.ENEMY;
        this.gameObject.SetActive(true);
        this.Rigid2d_.WakeUp();
    }

    protected override void onIdle()
    {
        this.gameObject.layer = LayerMgrs.DEAD;
    }

    protected override void onSleep()
    {
        this.gameObject.SetActive(false);
        this.Rigid2d_.Sleep();
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        var otherTag = collision.gameObject.tag;
        var disX = collision.gameObject.transform.localPosition.x - this.transform.localPosition.x;
        var oriValue = (disX < 0) ? 1 : -1;

        switch (otherTag) {

            case TagsMgr.GROUND:
                this.switchMoveOri();
                break;

            case TagsMgr.PLAYER_BULLET:
                this.onHitByBullet(oriValue);
                break;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        var otherTag = collision.gameObject.tag;
        var disX = collision.gameObject.transform.localPosition.x - this.transform.localPosition.x;
        var oriValue = (disX < 0) ? 1 : -1;

        switch (otherTag)
        {
            case TagsMgr.PLAYER_BULLET:
                this.onHitByBomb(oriValue);
                break;
        }
    }

    private void onHitByBullet(float horiValue) {
        
        if (this.changeHealth(50)) {
            this.GetComponent<ActorHuman>().Spark(0.5f);
        } else {
            this.State = EnumItemState.E_SLEEP;
            var pos = this.transform.localPosition;
            PrefabsMgr.GetInstance().Send(
                PrefabsMgr.TYPE_BOMB, 
                new WakeUpConfigBomb(pos,new Vector2(1,0),0.25f)
                );

            PrefabsMgr.GetInstance().Send(
                PrefabsMgr.TYPE_ENEMY_BODY,
                new WakeUpConfigDeadBody(pos, new Vector2(1* horiValue, 0),SpritesMgr.GetInstance().SpEnemyDeadBody)
                );
        }
    }

    private void onHitByBomb(float horiValue) {
        if (this.changeHealth(30))
        {
            this.GetComponent<ActorHuman>().Spark(0.5f);
        }
        else
        {
            this.State = EnumItemState.E_SLEEP;

            var pos = this.transform.localPosition;
            PrefabsMgr.GetInstance().Send(
            PrefabsMgr.TYPE_ENEMY_BODY,
            new WakeUpConfigStartPosAndOri(pos, new Vector2(1 * horiValue, 0))
            );
        }
    }
}
